package com.example.agriculyural;


import com.example.agriculyural.Decoraror.AutoAvoidanceDecorator;
import com.example.agriculyural.Decoraror.GPSDecorator;
import com.example.agriculyural.Facade.SmartFarmSystemFacade;
import com.example.agriculyural.FactoryMethod.*;
import com.example.agriculyural.Observer.EnvironmentData;

public class Main {

    // 兼容Java 8的String.repeat()方法
    public static String repeat(char c, int count) {
        StringBuilder sb = new StringBuilder(count);
        for (int i = 0; i < count; i++) {
            sb.append(c);
        }
        return sb.toString();
    }

    public static void main(String[] args) {
        System.out.println("========================================");
        System.out.println("    智能无人驾驶农机系统启动");
        System.out.println("========================================");

        // 创建系统外观
        SmartFarmSystemFacade farmSystem = new SmartFarmSystemFacade();

        // 使用工厂创建农机设备
        MachineFactory seedingFactory = new SeedingMachineFactory();
        MachineFactory sprayingFactory = new SprayingMachineFactory();
        MachineFactory harvestingFactory = new HarvestingMachineFactory();

        AgriculturalMachine seeder1 = seedingFactory.createMachine("SD-001");
        AgriculturalMachine sprayer1 = sprayingFactory.createMachine("SP-001");
        AgriculturalMachine harvester1 = harvestingFactory.createMachine("HV-001");

        // 使用装饰器增强功能
        AgriculturalMachine enhancedSeeder = new AutoAvoidanceDecorator(
                new GPSDecorator(seeder1)
        );
        AgriculturalMachine enhancedSprayer = new GPSDecorator(sprayer1);

// 添加设备到系统
        farmSystem.addMachine(enhancedSeeder);
        farmSystem.addMachine(enhancedSprayer);
        farmSystem.addMachine(harvester1);

        // 显示初始状态
        farmSystem.showSystemStatus();

        // 启动所有设备
        farmSystem.startAllMachines();

        // 模拟环境数据更新
        System.out.println("\n" + repeat('=', 50));
        System.out.println("场景1: 正常环境条件");
        EnvironmentData normalData = new EnvironmentData(45.0, 25.0, 60.0, 8.0, 6.5);
        farmSystem.updateEnvironmentData(normalData);

        System.out.println("\n" + repeat('=', 50));
        System.out.println("场景2: 高湿度环境");
        EnvironmentData wetData = new EnvironmentData(85.0, 22.0, 80.0, 5.0, 6.8);
        farmSystem.updateEnvironmentData(wetData);

        System.out.println("\n" + repeat('=', 50));
        System.out.println("场景3: 大风天气");
        EnvironmentData windyData = new EnvironmentData(55.0, 20.0, 45.0, 18.0, 6.2);
        farmSystem.updateEnvironmentData(windyData);

        // 模拟紧急情况
        System.out.println("\n" + repeat('=', 50));
        farmSystem.handleEmergency("检测到障碍物");

        System.out.println("\n" + repeat('=', 50));
        farmSystem.handleEmergency("设备故障");

        System.out.println("\n" + repeat('=', 50));
        farmSystem.handleEmergency("紧急停止");

        // 显示最终状态
        farmSystem.showSystemStatus();


    }
}